package myproject_svn;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XmlManager {

	public void xmlSplit(String inputFile, String path) throws Exception {
		File input = new File(inputFile.toString());
		DocumentBuilderFactory docbuildfact = DocumentBuilderFactory.newInstance();
		Document doc = docbuildfact.newDocumentBuilder().parse(input);
		
		XPath xpath = XPathFactory.newInstance().newXPath();

		NodeList nodes = (NodeList) xpath.evaluate(path, doc, XPathConstants.NODESET);
		int number = 1;
		Document currentDoc = docbuildfact.newDocumentBuilder().newDocument();
		Node rootNode = currentDoc.createElement("update");
		File currentFile = new File(number+".xml");
		for (int i=1; i <= nodes.getLength(); i++) {
			Node imported = currentDoc.importNode(nodes.item(i-1), true);
			rootNode.appendChild(imported);

			if (i % number == 0) {
				writeToFile(rootNode, currentFile);

				rootNode = currentDoc.createElement("updates");
				currentFile = new File((++number)+".xml");
			}
		}

		writeToFile(rootNode, currentFile);
	}

	public void constructXmlDom(String[] input) {
		try {
			 
			String[] line = new String[2];
			DocumentBuilderFactory dFact = DocumentBuilderFactory.newInstance();
			DocumentBuilder build = dFact.newDocumentBuilder();
			Document doc = build.newDocument();
			Element root = doc.createElement("root");
			doc.appendChild(root);
			Element memberList = doc.createElement("members");
			root.appendChild(memberList);
			for (int i = 0; i < input.length; i++) {
				line = input[i].split(",");
				Element member = doc.createElement("member");
				memberList.appendChild(member);
				Element name = doc.createElement("name");
				name.appendChild(doc.createTextNode(line[0]));
				member.appendChild(name);
				Element phone = doc.createElement("phone");
				phone.appendChild(doc.createTextNode(line[1]));
				member.appendChild(phone);
			}
			TransformerFactory tFact = TransformerFactory.newInstance();
			Transformer trans = tFact.newTransformer();

			StringWriter writer = new StringWriter();
			StreamResult result = new StreamResult(writer);
			DOMSource source = new DOMSource(doc);

			StreamResult result1 =  new StreamResult(new File("docXML.xml"));

			trans.transform(source, result1);
			trans.transform(source, result);
			System.out.println(writer.toString());

		} catch (TransformerException ex) {
			System.out.println("Error outputting document");
		} catch (ParserConfigurationException ex) {
			System.out.println("Error building document");
		}
	}

	public NodeList initializeNodes() throws ParserConfigurationException, SAXException, IOException
	{
		String configFile = "docXML.xml";
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();          
		Document doc = db.parse(configFile);
		doc.getDocumentElement().normalize();           
		NodeList layerConfigList = doc.getElementsByTagName("root");
		//Node node = layerConfigList.item(0);
		//Element e = (Element)node;
		return layerConfigList;
	}
	public void getAttributeDOM(NodeList nodes, String nodeName) throws ParserConfigurationException, SAXException, IOException
	{
		//NodeList nodes = initializeNodes();		
		//String nodeName = "tag";				
		Node n;
		Element e;
		String attr;
		for (int i = 0; i < nodes.getLength(); i++)
		{
			n = nodes.item(i);
			e = (Element)n;
			attr = e.getAttribute(nodeName);
			System.out.println(attr);

		}
	}
	public void parseDomDisplay(NodeList nodes, String nodeName) throws ParserConfigurationException, SAXException, IOException
	{
		NodeList nodeDOM;
		for (int i = 0; i < nodes.getLength(); i++ )
		{
			nodeDOM = nodes.item(i).getChildNodes();
			getAttributeDOM(nodeDOM, nodeName);
		}
	}

	public void writeToFile(Node node, File file) throws Exception {
		Transformer transformer = TransformerFactory.newInstance().newTransformer();
		transformer.transform(new DOMSource(node), new StreamResult(new FileWriter(file)));
	}
}

